package rexsee.script;

import rexsee.core.application.RexseeApplication;
import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.lang.RexseeLanguage;

public class RexseeScript implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Script";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	private final Browser mBrowser;

	private String mEncoding = "UTF-8";

	public RexseeScript(Browser browser) {
		super();
		mBrowser = browser;
	}

	public void debug(String url) {
		mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
		String content = mBrowser.urlListeners.getContentWithEncoding(mBrowser.urlListeners.getAbsoluteUrl(url), mEncoding);
		if (content == null || content.equalsIgnoreCase(RexseeApplication.EXCEPTION_PREFIX)) {
			mBrowser.application.alert(RexseeLanguage.MESSAGE_WEAK_NETWORK);
		} else {
			new ScriptParser(mBrowser).parse(content).debug();
		}
	}

	public String getEncoding() {
		return mEncoding;
	}
	public void setEncoding(String encoding) {
		mEncoding = encoding;
	}

}
